package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 处理品牌与类别关联数据业务的实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Service
@Slf4j
public class BrandCategoryServiceImpl implements IBrandCategoryService {

    @Autowired
    private BrandCategoryMapper brandCategoryMapper;

    @Override
    public void addNew(BrandCategoryAddNewDTO brandCategoryAddNewDTO) {
        log.debug("开始处理【添加品牌与类别的绑定】的业务，参数：{}", brandCategoryAddNewDTO);

        // 获取当前时间：LocalDateTime now = LocalDateTime.now()
        LocalDateTime now = LocalDateTime.now();
        // 创建对象
        BrandCategory brandCategory = new BrandCategory();
        // 复制品牌与类别的绑定
        BeanUtils.copyProperties(brandCategoryAddNewDTO, brandCategory);
        // 补全各品牌与类别的绑定的值：gmtCreate / gmtModified
        brandCategory.setGmtCreate(now);
        brandCategory.setGmtModified(now);
        // 执行插入数据
        int rows = brandCategoryMapper.insert(brandCategory);
        if (rows != 1) {
            String message = "添加品牌与类别的绑定失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除品牌与类别的绑定】的业务，参数：{}", id);
        // 检查尝试删除的品牌与类别的绑定是否存在
        Object queryResult = brandCategoryMapper.getStandardById(id);
        if (queryResult == null) {
            throw new ServiceException(ERR_NOT_FOUND,"删除品牌与类别的绑定失败，尝试访问的数据不存在！");
        }

        // 执行删除
        log.debug("即使删除id为{}的品牌与类别的绑定……", id);
        int rows = brandCategoryMapper.deleteById(id);
        if (rows != 1) {
            throw new ServiceException(ERR_DELETE,"删除品牌与类别的绑定失败，服务器忙，请稍后再次尝试！");
        }
        log.debug("删除完成！");
    }

}
